package cn.software.controller;

import cn.software.entity.Subject;
import cn.software.entity.param.ClassParam;
import cn.software.entity.param.SubjectParam;
import cn.software.entity.vo.ClassVo;
import cn.software.entity.vo.InvCodeVo;
import cn.software.entity.vo.SubjectVo;
import cn.software.service.ClassService;
import cn.software.service.SubjectService;
import cn.software.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;

import static java.util.UUID.randomUUID;

@RestController
@RequestMapping("/class")
@Api(tags = "班级接口")
public class ClassController {

    @Autowired
    private ClassService classService;

    @PostMapping("/addClass")
    @ApiOperation("增加班级")
    public Result addClass(@RequestBody ClassParam classParam) {
        String invCode=UUID.randomUUID().toString();
        classParam.setInvCode(invCode);
        boolean isSuccess = classService.addClass(classParam);
        if(isSuccess) {
            return Result.success(null);
        } else {
            return Result.fail("添加失败");
        }
    }


    @PostMapping("/updateClass")
    @ApiOperation("修改班级")
    public Result updateClass(@RequestBody ClassParam classParam){
        boolean isSuccess=classService.updateClass(classParam);
        if(isSuccess) {
            return Result.success(null);
        } else {
            return Result.fail("修改失败");
        }
    }

    @PostMapping("/deleteClass")
    @ApiOperation("删除班级")
    public Result deleteClass(@RequestBody ClassParam classParam){
        boolean isSuccess=classService.deleteClass(classParam);
        if(isSuccess) {
            return Result.success(null);
        } else {
            return Result.fail("删除失败");
        }
    }

    @GetMapping("/getClass")
    @ApiOperation("查询现有班级")
    public Result getAllClass(){
        List<ClassVo> list=classService.getAllClass();
        if(list!=null) {
            return Result.success(list);
        } else {
            return Result.fail("查询结果为空");
        }
    }

    @GetMapping("/getClassById")
    @ApiOperation("根据id查询学科信息")
    public Result getClassById(@RequestParam Integer id){
        ClassVo classVo=classService.getClassById(id);
        if(classVo!=null){
            return Result.success(classVo);
        }else {
            return Result.fail("查询结果为空");
        }
    }

    @GetMapping("/getInvCode")
    @ApiOperation("查邀请码")
    public Result getInvCode(@RequestParam Integer id){
        InvCodeVo invCodeVo =classService.getInvCode(id);
        if(invCodeVo!=null){
            return Result.success(invCodeVo);
        }else {
            return Result.fail("查询失败");
        }
    }

    @GetMapping("/getClassByStuId")
    @ApiOperation("根据id查询学科信息")
    public Result getClassByStuId(@RequestParam Integer id){
        List<ClassVo> list=classService.getClassByStuId(id);
        if(list!=null){
            return Result.success(list);
        }else {
            return Result.fail("查询结果为空");
        }
    }

}
